package com.iknow.image;

import android.graphics.Bitmap;
import android.os.AsyncTask.Status;
import android.widget.ImageView;
import com.iknow.IKnow;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.Loger;
import com.iknow.util.StringUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class ProfileImageCacheManager
{
  private static final String TAG = "ProfileImageCacheManager";
  private CallbackManager mCallbackManager;
  private HashMap<String, ProfileImageCacheCallback> mCallbackMap;
  private ImageManager mImageManager = new ImageManager(IKnow.mContext);
  private GenericTask mTask;
  private TaskListener mTaskListener;
  private ArrayList<String> mUrlList = new ArrayList();

  public ProfileImageCacheManager()
  {
    this.mCallbackMap = new HashMap();
    this.mCallbackManager = new CallbackManager();
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "GetProfileImage";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        if ((paramTaskResult == TaskResult.OK) && (ProfileImageCacheManager.access$0(this.this$0).size() != 0))
          this.this$0.doGetImage(null);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        super.onProgressUpdate(paramGenericTask, paramObject);
        TaskParams localTaskParams = (TaskParams)paramObject;
        String str = (String)localTaskParams.get("url");
        Bitmap localBitmap = (Bitmap)localTaskParams.get("bitmap");
        ProfileImageCacheManager.access$1(this.this$0).call(str, localBitmap);
      }
    };
  }

  public static ProfileImageCacheCallback createImageCallBack(ImageView paramImageView, String paramString)
  {
    return new ProfileImageCacheCallback(paramImageView)
    {
      public void refresh(, Bitmap paramBitmap)
      {
        if (paramString.equals(this.val$imageView.getTag()));
        try
        {
          Loger.i("ProfileImageCacheManager", "imageView.setImageBitmap");
          this.val$imageView.setImageBitmap(paramBitmap);
          return;
        }
        catch (RuntimeException localRuntimeException)
        {
          Loger.e("ProfileImageCacheManager", localRuntimeException.getMessage());
        }
      }
    };
  }

  private void putUrl(String paramString)
  {
    synchronized (this.mUrlList)
    {
      this.mUrlList.add(paramString);
      return;
    }
  }

  public void cancelImageTask()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
  }

  public void displayContentBimap(ImageView paramImageView, String paramString)
  {
    paramImageView.setTag(paramString);
    paramImageView.setImageBitmap(getContentBitmap(paramString, createImageCallBack(paramImageView, paramString)));
  }

  public void displayProfileBimap(ImageView paramImageView, String paramString)
  {
    paramImageView.setTag(paramString);
    paramImageView.setImageBitmap(getProfileImage(paramString, createImageCallBack(paramImageView, paramString)));
  }

  public void doGetImage(String paramString)
  {
    if (!(StringUtil.isEmpty(paramString)))
      putUrl(paramString);
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new GetImageTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  public Bitmap getContentBitmap(String paramString, ProfileImageCacheCallback paramProfileImageCacheCallback)
  {
    Bitmap localBitmap = this.mImageManager.get(paramString);
    if (localBitmap == ImageCache.mDefaultBitmap)
    {
      this.mCallbackManager.put(paramString, paramProfileImageCacheCallback);
      doGetImage(paramString);
      localBitmap = ImageCache.mDefaultBitmap;
    }
    return localBitmap;
  }

  public Bitmap getFromLocal(String paramString)
  {
    Loger.i("ProfileImageCacheManager", "get bitmap from local :" + paramString);
    return this.mImageManager.getFromLocal(paramString);
  }

  public ImageManager getImageManager()
  {
    return this.mImageManager;
  }

  public Bitmap getProfileImage(String paramString, ProfileImageCacheCallback paramProfileImageCacheCallback)
  {
    Loger.i("ProfileImageCacheManager", "get profile bitmap from :" + paramString);
    Bitmap localBitmap = this.mImageManager.get(paramString);
    if (localBitmap == ImageCache.mDefaultBitmap)
    {
      this.mCallbackManager.put(paramString, paramProfileImageCacheCallback);
      doGetImage(paramString);
      localBitmap = ImageCache.mDefaultProfileBitmap;
    }
    return localBitmap;
  }

  public void removeCallBack(String paramString)
  {
    if (this.mCallbackMap.containsKey(paramString))
      this.mCallbackMap.remove(paramString);
  }

  public static class CallbackManager
  {
    private static final String TAG = "CallbackManager";
    private ConcurrentHashMap<String, List<ProfileImageCacheCallback>> mCallbackMap = new ConcurrentHashMap();

    public void call(String paramString, Bitmap paramBitmap)
    {
      List localList = (List)this.mCallbackMap.get(paramString);
      if (localList == null)
      {
        Loger.e("CallbackManager", "callbackList=null");
        return;
      }
      Iterator localIterator = localList.iterator();
      while (true)
      {
        ProfileImageCacheCallback localProfileImageCacheCallback;
        do
        {
          while (!(localIterator.hasNext()))
          {
            localList.clear();
            this.mCallbackMap.remove(paramString);
          }
          localProfileImageCacheCallback = (ProfileImageCacheCallback)localIterator.next();
        }
        while (localProfileImageCacheCallback == null);
        localProfileImageCacheCallback.refresh(paramString, paramBitmap);
      }
    }

    public void put(String paramString, ProfileImageCacheCallback paramProfileImageCacheCallback)
    {
      if (!(this.mCallbackMap.containsKey(paramString)))
      {
        Loger.v("CallbackManager", "url does not exist, add list to map");
        this.mCallbackMap.put(paramString, new ArrayList());
      }
      ((List)this.mCallbackMap.get(paramString)).add(paramProfileImageCacheCallback);
      Loger.v("CallbackManager", "Add callback to list, count(url)=" + ((List)this.mCallbackMap.get(paramString)).size());
    }
  }

  private class GetImageTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      String str;
      if (ProfileImageCacheManager.access$0(this.this$0).size() <= 0)
        return TaskResult.OK;
      ArrayList localArrayList = ProfileImageCacheManager.access$0(this.this$0);
      monitorenter;
      try
      {
        str = (String)ProfileImageCacheManager.access$0(this.this$0).get(0);
        monitorexit;
      }
      finally
      {
        try
        {
          while (true)
          {
            ProfileImageCacheManager.access$2(this.this$0).put(str);
            TaskParams localTaskParams = new TaskParams();
            localTaskParams.put("url", str);
            localTaskParams.put("bitmap", ProfileImageCacheManager.access$2(this.this$0).get(str));
            Object[] arrayOfObject = new Object[1];
            arrayOfObject[0] = localTaskParams;
            publishProgress(arrayOfObject);
          }
          localObject = finally;
          monitorexit;
          throw localObject;
        }
        catch (IOException localIOException)
        {
          Loger.e("ProfileImageCacheManager", str + " get failed!");
        }
        catch (Exception localException)
        {
          Loger.e("ProfileImageCacheManager", str + " get failed!");
        }
      }
    }
  }
}